package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 购物车业务实现类
 *
 * @Author mr.lcc
 * @Data 2025/5/24 16:33
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 1.参数校验
        if (ObjectUtils.isEmpty(shoppingCartDTO)) {
            throw new BaseException("添加购物车参数为空！");
        }

        // 2.只添加到自己的购物车中
        Long userId = BaseContext.getCurrentId();
        ShoppingCart newShoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, newShoppingCart);
        newShoppingCart.setUserId(userId);

        // 4. 判断当前添加的菜品或套餐是否在售
        Long dishIdQuery = newShoppingCart.getDishId();
        if (dishIdQuery != null) {
            Dish dishOne = dishMapper.selectById(dishIdQuery);
            if (dishOne != null && dishOne.getStatus() == 0) {
                throw new BaseException("菜品暂时停售无法添加到购物车");
            }
        }

        Long setmealIdQuery = newShoppingCart.getSetmealId();
        if (setmealIdQuery != null) {
            Setmeal setmealOne = setmealMapper.selectById(setmealIdQuery);
            if (setmealOne == null) {
                throw new BaseException("套餐不存在，无法添加到购物车");
            }
            if (setmealOne.getStatus() == 0) {
                throw new BaseException("套餐暂时停售无法添加到购物车");
            }
        }


        // 5.判断当前添加的菜单或套餐是否在购物车中
        List<ShoppingCart> list = this.lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(newShoppingCart.getUserId()), ShoppingCart::getUserId, userId)
                .eq(ObjectUtils.isNotEmpty(newShoppingCart.getDishId()), ShoppingCart::getDishId, newShoppingCart.getDishId())
                .eq(ObjectUtils.isNotEmpty(newShoppingCart.getSetmealId()), ShoppingCart::getSetmealId, newShoppingCart.getSetmealId())
                .orderByDesc(ShoppingCart::getCreateTime)
                .list();

        // 6.如果是同一菜品就+1
        if (list != null && list.size() == 1) {
            ShoppingCart shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            this.updateById(shoppingCart);
        } else {
            // 7.如果不是就新增
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 添加到购物车的是菜品类型
                Dish dish = dishMapper.selectById(dishId);
                newShoppingCart.setName(dish.getName());
                newShoppingCart.setImage(dish.getImage());
                newShoppingCart.setAmount(dish.getPrice());
            } else {
                // 添加到购物车的是套餐类型
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                newShoppingCart.setName(setmeal.getName());
                newShoppingCart.setImage(setmeal.getImage());
                newShoppingCart.setAmount(setmeal.getPrice());
            }
            // 第一次添加到购物车为数量为1
            newShoppingCart.setNumber(1);
            // 8.新增到购物车
            this.save(newShoppingCart);
        }

    }

    /**
     * 删除根据用户id
     *
     * @return
     */
    @Override
    public void removeByUserId() {
        Long currentId = BaseContext.getCurrentId();
        boolean remove = this.lambdaUpdate()
                .eq(ShoppingCart::getUserId, currentId)
                .remove();
        if (remove) {
            throw new BaseException("删除购物车失败！");
        }
    }
}
